package com.gagakuai.engine.impl.pvm.process;

import com.gagakuai.engine.delegate.ExecutionListener;
import com.gagakuai.engine.impl.pvm.PvmActivity;
import com.gagakuai.engine.impl.pvm.PvmProcessDefinition;
import com.gagakuai.engine.impl.pvm.PvmProcessElement;
import com.gagakuai.engine.impl.pvm.PvmScope;

import java.util.*;

/*
 * @program: common-starters
 * @description: 范围抽象类
 * @author: houhong
 * @create: 2023-01-02 11:35
 */
public abstract class ScopeImpl extends ProcessElementImpl implements PvmScope {


    private static final long serialVersionUID = -1911838213450766959L;

    protected List<PvmActivity> activities = new ArrayList<>();
    protected Map<String, PvmActivity> namedActivities = new HashMap<>();

    /*
    *  监听器
    */
    protected Map<String, List<ExecutionListener>> executionListeners = new HashMap<>();

    public ScopeImpl(String id, ProcessDefinitionImpl processDefinition) {
        super(id, processDefinition);
    }

    public ScopeImpl(String id, ProcessDefinitionImpl processDefinition, Map<String, Object> properties) {
        super(id, processDefinition, properties);
    }

    @Override
    public PvmActivity findActivity(String activityId) {

        PvmActivity pvmActivity = namedActivities.get(activityId);

        if (pvmActivity != null) {
            return pvmActivity;
        }
        for (PvmActivity activity : activities) {
            pvmActivity = activity.findActivity(activityId);
            if (pvmActivity != null) {
                return pvmActivity;
            }
        }
        return null;
    }

    public List<ExecutionListener> getExecutionListeners(String eventName){

        List<ExecutionListener> executionListenerList = getExecutionListeners().get(eventName);
        if (executionListenerList!=null) {
            return executionListenerList;
        }
        return Collections.EMPTY_LIST;

    }

    public Map<String, List<ExecutionListener>> getExecutionListeners() {
        return executionListeners;
    }
}